Public Class BeheerKlantenForm

    Private _mainForm As Form

#Region "Public Property's"

    Private _kanWijzigen As Boolean
    Public Property KanWijzigen() As Boolean
        Get
            Return _kanWijzigen
        End Get
        Set(ByVal value As Boolean)
            _kanWijzigen = value
            tsbWijzigen.Enabled = value
        End Set
    End Property

    Private _kanVerwijderen As Boolean
    Public Property KanVerwijderen() As Boolean
        Get
            Return _kanVerwijderen
        End Get
        Set(ByVal value As Boolean)
            _kanVerwijderen = value
            tsbVerwijderen.Enabled = value
            BindingNavigatorDeleteItem.Enabled = value
        End Set
    End Property

    Private _kanBewaren As Boolean
    Public Property KanBewaren() As Boolean
        Get
            Return _kanBewaren
        End Get
        Set(ByVal value As Boolean)
            _kanBewaren = value
            tsbBewaren.Enabled = value
        End Set
    End Property

#End Region

#Region "Constructor"

    Public Sub New(ByVal mainForm As Form)
        Me.InitializeComponent()
        _mainForm = mainForm
    End Sub

    Protected Overrides Sub OnLoad(ByVal e As EventArgs)
        MyBase.OnLoad(e)
        Try
            Me.KlantenBindingSource.DataSource = Utility.Data
            InitGrid()
            KanBewaren = False
            KanVerwijderen = False
            KanWijzigen = False
        Catch ex As Exception
            MsgBox.ShowError("Ophalen data is mislukt")
            log.WriteError("BeheerKlantenForm.Load", ex)
        End Try
    End Sub

#End Region

#Region "Event handling"

    Protected Overrides Sub OnKeyUp(ByVal e As KeyEventArgs)
        MyBase.OnKeyUp(e)
        Select Case e.KeyCode
            Case Keys.F12
                Me.Close()
            Case Keys.F2
                Me.KlantenBindingSource.EndEdit()
                If (Not KanBewaren) Then Return
                Bewaar()
            Case Keys.F3
                If (Not KanVerwijderen) Then Return
                Verwijderen()
            Case Keys.F4
                NieuweKlant()
            Case Keys.F5
                If (Not KanWijzigen) Then Return
                If (Grid1.SelectedRows.Count > 0) Then
                    BeheerKlant(Grid1.SelectedRows(0).Index)
                End If
        End Select
    End Sub

    Private Sub tsbNieuw_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbNieuw.Click
        NieuweKlant()
    End Sub

    Private Sub tsbWijzigen_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbWijzigen.Click
        If (Grid1.SelectedRows.Count > 0) Then
            BeheerKlant(Grid1.SelectedRows(0).Index)
        End If
    End Sub

    Private Sub tsbVerwijderen_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbVerwijderen.Click
        Verwijderen()
    End Sub

    Private Sub tsbBewaren_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbBewaren.Click
        Bewaar()
    End Sub

    Private Sub tsbSluiten_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbSluiten.Click
        Me.Close()
    End Sub

    Protected Overrides Sub OnFormClosing(ByVal e As FormClosingEventArgs)
        MyBase.OnFormClosing(e)
        Me.Validate()
        Me.KlantenBindingSource.EndEdit()
        If (Utility.HasChanges(Utility.Data.Klanten)) Then
            Dim result As DialogResult = MessageBox.Show("De klanten zijn veranderd. Wil je de wijzigingen bewaren?", "Bewaren", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation)
            Select Case result
                Case Windows.Forms.DialogResult.Yes
                    Bewaar()
                Case Windows.Forms.DialogResult.Cancel
                    e.Cancel = True
                    Return
                Case Windows.Forms.DialogResult.No
                    ' annuleer de wijzigingen en laat sluiten
                    Utility.Data.Klanten.RejectChanges()
            End Select
        End If
        _mainForm.BringToFront()
    End Sub

    Private Sub Grid1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Grid1.CurrentCellDirtyStateChanged
        If Grid1.IsCurrentCellDirty Then
            KanBewaren = True
        End If
    End Sub

    Private Sub Grid1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Grid1.SelectionChanged
        KanWijzigen = (Grid1.SelectedRows.Count > 0)
        KanVerwijderen = (Grid1.SelectedRows.Count > 0)
    End Sub

    Private Sub Grid1_CellDoubleClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles Grid1.CellDoubleClick
        BeheerKlant(e.RowIndex)
    End Sub

    Private Sub Grid1_DataError(ByVal sender As System.Object, ByVal e As DataGridViewDataErrorEventArgs) Handles Grid1.DataError
        MsgBox.ShowError("De ingevoerde gegevens zijn niet geldig.")
        log.WriteError("BeheerKlantenForm.Grid1_DataError", e.Exception)
        e.Cancel = True
    End Sub

    Private Sub Grid1_RowsRemoved(ByVal sender As Object, ByVal e As DataGridViewRowsRemovedEventArgs) Handles Grid1.RowsRemoved
        ' nodig omdat je ook kan verwijderen door Del te drukken in de grid
        KanBewaren = True
    End Sub

#End Region

#Region "Helperfuncties"

    Private Sub InitGrid()
        With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Klanten.IdColumn.ColumnName, Grid1)
            .Visible = False ' Autonumber niet tonen
        End With
        With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Klanten.NaamColumn.ColumnName, Grid1)
            .Width = 300
        End With
        With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Klanten.AdresColumn.ColumnName, Grid1)
            .Width = 100
        End With
        With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Klanten.PostcodeColumn.ColumnName, Grid1)
            .Width = 100
        End With
        With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Klanten.GemeenteColumn.ColumnName, Grid1)
            .Width = 100
        End With
        With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Klanten.TelefoonColumn.ColumnName, Grid1)
            .Width = 100
        End With
        With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Klanten.EmailColumn.ColumnName, Grid1)
            .Width = 100
            .HeaderText = "E-mail"
        End With
        With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Klanten.KlantenkaartColumn.ColumnName, Grid1)
            .Width = 60
        End With
    End Sub

    Private Sub NieuweKlant()
        Try
            Cursor = Cursors.WaitCursor
            Dim insertForm As New KlantenToevoegForm() ' TODO: beter met BeheerKlantenForm (meer velden)
            If (insertForm.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                ' update grid gaat vanzelf
                KanBewaren = True
                KlantenBindingSource.Position = KlantenBindingSource.Find("ID", insertForm.IdKlant)
            End If
        Catch ex As Exception
            log.WriteError("BeheerKlantenForm.NieuweKlant", ex)
        Finally
            Cursor = Cursors.Default
        End Try
    End Sub

    Private Shadows Function Bewaar() As Integer
        Return MyBase.Bewaar(Utility.Data.Klanten, "klant(en)", KanBewaren)
    End Function

    Private Sub Verwijderen()
        Try
            If (Me.KlantenBindingSource.Current IsNot Nothing) Then
                Me.KlantenBindingSource.RemoveCurrent()
                KanBewaren = True
            End If
        Catch ex As Exception
            log.WriteError("BeheerKlantenForm.Verwijderen" & Environment.NewLine & ex.ToString())
        End Try
    End Sub

    Private Sub BeheerKlant(ByVal rowIndex As Integer)
        Try
            Cursor = Cursors.WaitCursor
            Dim klantRow As ArtikelsDataSet.KlantenRow = Utility.GetGridDataRow(Of ArtikelsDataSet.KlantenRow)(Grid1, rowIndex)
            If (klantRow Is Nothing) Then Return ' bijvoorbeeld bij dubbelklikken op lege rij onderaan

            Dim editForm As New BeheerKlantForm(klantRow.Id)
            If (editForm.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                ' update grid gaat vanzelf
            End If
        Catch ex As Exception
            MsgBox.ShowError("Fout tijdens het wijzigen van de klant")
            log.WriteError("BeheerKlantenForm.BeheerKlant", ex)
        Finally
            Cursor = Cursors.Default
        End Try
    End Sub

#End Region

End Class